// index.ts
import { StorageType, setItem, getItem, removeItem, clearStorage, getAllKeys } from './storageUtils'

/**
 * localStorage相关操作
 */
export const local = {
  /**
   * 设置localStorage项
   * @param key 键名
   * @param value 值
   */
  set: <T>(key: string, value: T): void => setItem(StorageType.LOCAL, key, value),

  /**
   * 获取localStorage项
   * @param key 键名
   * @param defaultValue 默认值
   * @returns 存储的值或默认值
   */
  get: <T>(key: string, defaultValue?: T): T | null =>
    getItem(StorageType.LOCAL, key, defaultValue),

  /**
   * 移除localStorage项
   * @param key 键名
   */
  remove: (key: string): void => removeItem(StorageType.LOCAL, key),

  /**
   * 清空localStorage
   */
  clear: (): void => clearStorage(StorageType.LOCAL),

  /**
   * 获取所有localStorage键名
   * @returns 键名数组
   */
  keys: (): string[] => getAllKeys(StorageType.LOCAL),
}

/**
 * sessionStorage相关操作
 */
export const session = {
  /**
   * 设置sessionStorage项
   * @param key 键名
   * @param value 值
   */
  set: <T>(key: string, value: T): void => setItem(StorageType.SESSION, key, value),

  /**
   * 获取sessionStorage项
   * @param key 键名
   * @param defaultValue 默认值
   * @returns 存储的值或默认值
   */
  get: <T>(key: string, defaultValue?: T): T | null =>
    getItem(StorageType.SESSION, key, defaultValue),

  /**
   * 移除sessionStorage项
   * @param key 键名
   */
  remove: (key: string): void => removeItem(StorageType.SESSION, key),

  /**
   * 清空sessionStorage
   */
  clear: (): void => clearStorage(StorageType.SESSION),

  /**
   * 获取所有sessionStorage键名
   * @returns 键名数组
   */
  keys: (): string[] => getAllKeys(StorageType.SESSION),
}

export default {
  local,
  session,
}
